$(function () {
    $('body').on('click', '#sendCaptcha', function () { //发送验证码
        var _that = this;
        $(_that).attr("disabled", true);
        $(_that).css("pointer-events","none");
        $.ajax({
            url: baseloaction + '/sendCaptcha',
            data: {'account': $('input[name=account]').val()},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                if (response.success) {
                    $('#second').parents('span').removeClass('hide');
                    $(_that).addClass('hide');
                    var seconds = 60;
                    var interval = setInterval(function () {
                        $('#second').html(--seconds);
                        if (seconds == 0) {
                            $('#second').html('60').parents('span').addClass('hide');
                            $(_that).removeClass('hide');
                            relieveSend(_that)
                            clearInterval(interval);
                        }
                    }, 1000);
                } else {
                    relieveSend(_that)
                    $('#errorTips').html(response.message).parents('div.alert-warning').show();
                }
            },
            error: function () {
                relieveSend(_that)
                $('#errorTips').html('系统繁忙，发送验证码失败').parents('div.alert-warning').show();
            }
        });
    }).on('click', '#applyAccount', function () {
        var _this = $(this);
        _this.attr("disabled","disabled");
        $.ajax({
            url: baseloaction + '/doRegister',
            data: $('#applyForm').serialize(),
            dataType: 'json',
            type: 'post',
            success: function (response) {
                _this.removeAttr("disabled");
                if (response.success) {
                    $('#applyForm input').val('');
                    layer.confirm(response.message, {
                        btn: '知道了'
                        ,cancel: function(index, layero){
                        //取消操作，点击右上角的x
                            window.location = baseloaction + "/login";
                    }
                }, function () {
                        window.location = baseloaction + "/login";
                    });
                } else {
                    $('#errorTips').html(response.message).parents('div.alert-warning').show();
                }
            },
            error: function () {
                _this.removeAttr("disabled");
                $('#errorTips').html('系统错误，申请失败').parents('div.alert-warning').show();
            }
        })
    }).on('focus', 'div.input-group input', function () {
        $('div.alert-warning').hide();
    });

    $('.signin-form-bx').on('click', '.input-group-addon', function () {
        $(this).next('.form-control').focus();
    })


    function  relieveSend(that){
        $(that).removeAttr("disabled");
        $(that).css("pointer-events","");
    }
});
